package io.renren.modules.PCguanli.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.renren.modules.app.user.UserPicUpdateVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.PCguanli.dao.DriversDatcherDao;
import io.renren.modules.PCguanli.entity.DriversDatcherEntity;
import io.renren.modules.PCguanli.service.DriversDatcherService;
import org.springframework.transaction.annotation.Transactional;


@Service("driversDatcherService")
public class DriversDatcherServiceImpl extends ServiceImpl<DriversDatcherDao, DriversDatcherEntity> implements DriversDatcherService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String) params.get("key");
        LambdaQueryWrapper<DriversDatcherEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(key), DriversDatcherEntity::getName, key)
                .or().like(StringUtils.isNotEmpty(key), DriversDatcherEntity::getId, key)
                .or().like(StringUtils.isNotEmpty(key), DriversDatcherEntity::getUserId, key)
                .or().like(StringUtils.isNotEmpty(key), DriversDatcherEntity::getDatetime, key)
                .or().like(StringUtils.isNotEmpty(key), DriversDatcherEntity::getDatetimevli, key)
                .or().like(StringUtils.isNotEmpty(key), DriversDatcherEntity::getEmissionstandard, key)
                .or().like(StringUtils.isNotEmpty(key), DriversDatcherEntity::getDrivernumber, key)
                .or().like(StringUtils.isNotEmpty(key), DriversDatcherEntity::getCardealer, key);
        IPage<DriversDatcherEntity> page = this.page(
                new Query<DriversDatcherEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Autowired
    private DriversDatcherDao driversDatcherDao;

    @Override
    public DriversDatcherEntity info(int userId) {
        return driversDatcherDao.selectOne(new LambdaQueryWrapper<DriversDatcherEntity>()
                .eq(DriversDatcherEntity::getUserId, userId));
//      return  this.getObj(new LambdaQueryWrapper<DriversDatcherEntity>().eq(DriversDatcherEntity::getUserId,userId));
    }

    //更新人员信息
    public DriversDatcherEntity updateDriversDatcher(int userId, String name, String vehiclenumber, String iphone, String id) {
        // 此处应根据你的Dao接口中提供的方法进行调整
        DriversDatcherEntity driversDatcherEntity = driversDatcherDao.selectById(userId);
        if (driversDatcherEntity != null) {
            driversDatcherEntity.setId(id);
            driversDatcherEntity.setName(name);
            driversDatcherEntity.setIphone(iphone);
            driversDatcherEntity.setVehiclenumber(vehiclenumber);
            //driversDatcherEntity.setDatetime(datetime);
            driversDatcherDao.updateById(driversDatcherEntity);
        } else {
            throw new RuntimeException("User not found with id " + userId);
        }
        return driversDatcherEntity;
    }

    @Override
    public DriversDatcherEntity updateUserPic(UserPicUpdateVO userPicUpdate) {
        return updateDriversDatcherWithPictures(userPicUpdate.getUserId(), userPicUpdate.getPicture1(), userPicUpdate.getPicture2(), userPicUpdate.getPicture3(), userPicUpdate.getPicture4());
    }

    @Transactional
    public DriversDatcherEntity updateDriversDatcherWithPictures(int userId, String picture1, String picture2, String picture3, String picture4) {
        // 此处应根据你的Dao接口中提供的方法进行调整
        DriversDatcherEntity driversDatcherEntity = driversDatcherDao.selectById(userId);

        if (driversDatcherEntity != null) {
            driversDatcherEntity.setPicture1(picture1);
            driversDatcherEntity.setPicture2(picture2);
            driversDatcherEntity.setPicture3(picture3);
            driversDatcherEntity.setPicture4(picture4);

            driversDatcherDao.updateById(driversDatcherEntity);
        } else {
            throw new RuntimeException("User not found with id " + userId);
        }

        return driversDatcherEntity;
    }
}